<template>
  <div>
    <Drawer
      ref="modal"
      :title="`${editType === 'create' ?  $t('permissionsIndex.roleAdd') :  $t('permissionsIndex.roleEdit')}`"
      :width="500"
      :value="show"
      :beforeClose="$_modal_decideIfClose"
    >

      <Form
        ref="form"
        :model="formData"
        :labelWidth="100"
        :rules="formRule"
      >
        <FormItem prop="RoleName" :label="$t('permissionsIndex.roleTableName')">
          <Input v-model.trim="formData.RoleName" :placeholder="$t('permissionsIndex.roleInputName')" />
        </FormItem>
        <FormItem prop="RoleMark" :label="$t('permissionsIndex.roleRemarkAdd')">
          <Input v-model.trim="formData.RoleMark"  :placeholder="$t('permissionsIndex.roleInputRemark')" />
        </FormItem>
        <FormItem prop="dataPermiss" :label="$t('permissionsIndex.rolepermissionsSel')">
          <Tree 
            style="margin-top:-6px"
            @on-check-change ='TreeChange'
            ref="tree"
             :data="dataPermiss" show-checkbox>
          </Tree>
        </FormItem>
        <FormItem class="glo-btn-group">
          <Button
            type="primary"
            :loading="loading"
            @click="handleSubmit"
          >{{ $t("devicesCreate.devCreateSubmit") }}</Button>
          <Button
            @click="$_modal_close"
          >{{ $t("devicesCreate.devCreateCancel") }}</Button>
        </FormItem>
      </Form>
    </Drawer>
  </div>
</template>

<script>
import {
  mapState,
} from 'vuex'
import _ from 'lodash'
import moment from 'moment'

import modalMixin from '@/utils/mixins/modalMixin.js'
import {
  create,
  update,
} from '@/api/Role.js'
import {
  getPhoto,
} from '@/api/public.js'

export default {
  mixins: [
    modalMixin,
  ],

  props: {
    editType: {
      type: String,
      default: 'create',
    },
    editItem: Object,
    parentDepart: [String, Number],
  },
  data () {
    return {
      dataPermissData: [],
      formData: {
        RoleName: '',
        RoleMark: '',
      },
      dataPermiss: [
        {
          id:56,
          parentId:0,
          title: this.$t('navigationLeft.newDevices'),
          sort: 0,
          expand: false,
          permission: '#newDevices',
          children: [
            {
              id:57,
              parentId:56,
              title: this.$t('newDevices.site'),
              sort: 1,
              expand: true,
              permission: '#site',
              children: [
                {
                  id:58,
                  parentId:57,
                  title: this.$t('newDevices.settingAdmin'),
                  sort: 1,
                  expand: true,
                  permission: '#settingAdmin',
                },
                {
                  id:59,
                  parentId:57,
                  title: this.$t('newDevices.creatSite'),
                  sort: 2,
                  expand: true,
                  permission: '#creatSite',
                },
                {
                  id:60,
                  parentId:57,
                  title: this.$t('staffIndex.staffDepartmentAddChildEdit'),
                  sort: 3,
                  expand: true,
                  permission: '#staffDepartmentAddChildEdit',
                },
                {
                  id:61,
                  parentId:57,
                  title: this.$t('staffIndex.staffDepartmentAddChildDel'),
                  sort: 4,
                  expand: true,
                  permission: '#staffDepartmentAddChildDel',
                },
              ],
            },
            {
              id:62,
              parentId:56,
              title: this.$t('companyManagement.administrator'),
              sort: 2,
              expand: true,
              permission: '#administrator',
              // children: [
              //   {
              //     id:63,
              //     parentId:62,
              //     title: this.$t('versionIndex.verListSettingsEdit'),
              //     sort: 1,
              //     expand: true,
              //     permission: '#verListSettingsEdit7',
              //   },
              //   {
              //     id:64,
              //     parentId:62,
              //     title: this.$t('versionIndex.verListSettingsDel'),
              //     sort: 2,
              //     expand: true,
              //     permission: '#verListSettingsDel7',
              //   },
              // ],
            },
            {
              id:65,
              parentId:56,
              title: this.$t('staffIndex.staffDepartCrewList'),
              sort: 3,
              expand: true,
              permission: '#staffDepartCrewList',
              children: [
                {
                  id:66,
                  parentId:65,
                  title: this.$t('versionIndex.verAddBtn'),
                  sort: 1,
                  expand: true,
                  permission: '#verAddBtn2',
                },
                {
                  id:67,
                  parentId:65,
                  title: this.$t('staffIndex.stafferrorDel'),
                  sort: 2,
                  expand: true,
                  permission: '#stafferrorDel4',
                },
                {
                  id:68,
                  parentId:65,
                  title: this.$t('newDevices.jurisdiction'),
                  sort: 3,
                  expand: true,
                  permission: '#verListSettingsEdit8',
                },
                {
                  id:69,
                  parentId:65,
                  title: this.$t('versionIndex.verListSettingsDel'),
                  sort: 4,
                  expand: true,
                  permission: '#verListSettingsDel8',
                },
              ],
            },
            {
              id:77,
              parentId:56,
              title: this.$t('devicesIndex.devStrEquipment'),
              sort: 4,
              expand: true,
              permission: '#devList',
              children: [
                {
                  id:78,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListEdit'),
                  sort: 1,
                  expand: true,
                  permission: '#devListEdit3',
                },
                {
                  id:79,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListRelieve'),
                  sort: 2,
                  expand: true,
                  permission: '#devListRelieve2',
                },
                {
                  id:80,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListCut'),
                  sort: 3,
                  expand: true,
                  permission: '#devListCut2',
                },
                {
                  id:81,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListrestart'),
                  sort: 4,
                  expand: true,
                  permission: '#devListrestart2',
                },
                {
                  id:82,
                  parentId:77,
                  title: this.$t('devicesListSettings.devListDel'),
                  sort: 5,
                  expand: true,
                  permission: '#devListDel2',
                },
                {
                  id:83,
                  parentId:77,
                  title: this.$t('devicesCreate.devTimezones'),
                  sort: 6,
                  expand: true,
                  permission: '#devTimezones2',
                },
                {
                  id:84,
                  parentId:77,
                  title: this.$t('newDevices.addEquipments'),
                  sort: 7,
                  expand: true,
                  permission: '#addEquipments',
                },
                {
                  id:86,
                  parentId:77,
                  title: this.$t('devicesIndex.restoreSetting'),
                  sort: 8,
                  expand: true,
                  permission: '#restoreSetting1',
                },
              ],
            },
          ],
        },
        {
          id:1,
          parentId:0,
          title: this.$t('navigationLeft.equipment'),
          sort: 1,
          expand: false,
          permission: '#device',
          children: [
            {
              id:2,
              parentId:1,
              title: this.$t('devicesListSettings.devListEdit'),
              sort: 1,
              expand: true,
              permission: '#devListEdit',
            },
            // {
            //   id:3,
            //   parentId:1,
            //   title: this.$t('devicesListSettings.devListRelieve'),
            //   sort: 2,
            //   expand: true,
            //   permission: '#devListRelieve',
            // },
            {
              id:4,
              parentId:1,
              title: this.$t('devicesListSettings.devListCut'),
              sort: 3,
              expand: true,
              permission: '#devListCut',
            },
            {
              id:5,
              parentId:1,
              title: this.$t('devicesListSettings.devListrestart'),
              sort: 4,
              expand: true,
              permission: '#devListrestart',
            },
            {
              id:6,
              parentId:1,
              title: this.$t('devicesListSettings.devListDel'),
              sort: 5,
              expand: true,
              permission: '#devListDel',
            },
            {
              id:7,
              parentId:1,
              title: this.$t('devicesCreate.devTimezones'),
              sort: 6,
              expand: true,
              permission: '#devTimezones',
            },
            {
              id:8,
              parentId:1,
              title: this.$t('devicesIndex.devOfflineHint'),
              sort: 7,
              expand: true,
              permission: '#devOfflineHint',
            },
            {
              id:9,
              parentId:1,
              title: this.$t('devicesIndex.devUpgradeBtn'),
              sort: 8,
              expand: true,
              permission: '#devUpgradeBtn',
            },
            {
              id:10,
              parentId:1,
              title: this.$t('devicesIndex.restoreSetting'),
              sort: 9,
              expand: true,
              permission: '#restoreSetting',
            },
            // {
            //   id:10,
            //   parentId:1,
            //   title: this.$t('devicesIndex.devBingBtn'),
            //   sort: 9,
            //   expand: true,
            //   permission: '#devBingBtn',
            // },
          ],
        },
        {
          id:11,
          parentId:0,
          title: this.$t('navigationLeft.staff'),
          sort: 2,
          expand: false,
          permission: '#staff',
          children: [
            // {
            //   title: this.$t('AccessIndex.AccessJobdeNum'),
            //   sort: 1,
            //   expand: true,
            //   permission: '#AccessJobdeNum',
            //   children: [
            //     {
            //       title: this.$t('AccessIndex.AccessJobdeNum'),
            //       sort: 1,
            //       expand: true,
            //       permission: '#AccessJobdeNum',
            //     },
            //     {
            //       title: this.$t('staffIndex.staffSuccessList'),
            //       sort: 2,
            //       expand: true,
            //       permission: '#staffSuccessList',
            //     },
            //     {
            //       title: this.$t('staffIndex.stafferrorList'),
            //       sort: 3,
            //       expand: true,
            //       permission: '#stafferrorList',
            //     },
            //   ],
            // },
            {
              id:12,
              parentId:11,
              title: this.$t('staffIndex.staffSuccessList'),
              sort: 2,
              expand: true,
              permission: '#staffSuccessList1',
              children: [
                {
                  id:13,
                  parentId:12,
                  title: this.$t('staffIndex.staffTransfer'),
                  sort: 1,
                  expand: true,
                  permission: '#staffTransfer',
                },
                {
                  id:14,
                  parentId:12,
                  title: this.$t('staffIndex.staffBatchUpload'),
                  sort: 2,
                  expand: true,
                  permission: '#staffBatchUpload',
                },
                {
                  id:15,
                  parentId:12,
                  title: this.$t('staffIndex.stafferrorDel'),
                  sort: 3,
                  expand: true,
                  permission: '#stafferrorDel',
                },
                {
                  id:16,
                  parentId:12,
                  title: this.$t('versionIndex.verListSettingsEdit'),
                  sort: 4,
                  expand: true,
                  permission: '#verListSettingsEdit',
                },
                {
                  id:17,
                  parentId:12,
                  title: this.$t('versionIndex.verListSettingsDel'),
                  sort: 5,
                  expand: true,
                  permission: '#verListSettingsDel',
                },
                {
                  id:18,
                  parentId:12,
                  title: this.$t('staffIndex.staffeIDcardBtn'),
                  sort: 6,
                  expand: true,
                  permission: '#staffeIDcardBtn',
                },
                {
                  id:19,
                  parentId:12,
                  title: this.$t('staffIndex.staffDepartCreateAdd'),
                  sort: 7,
                  expand: true,
                  permission: '#staffDepartCreateAdd',
                },
                {
                  id:20,
                  parentId:12,
                  title: this.$t('staffIndex.staffclickToDow'),
                  sort: 8,
                  expand: true,
                  permission: '#staffclickToDow',
                },
                {
                  id:21,
                  parentId:12,
                  title: this.$t('staffIndex.staffclickToDowIDCard'),
                  sort: 9,
                  expand: true,
                  permission: '#staffclickToDowIDCard',
                },
              ],
            },
            {
              id:22,
              parentId:11,
              title: this.$t('staffIndex.stafferrorList'),
              sort: 3,
              expand: true,
              permission: '#stafferrorList1',
              children: [
                {
                  id:23,
                  parentId:22,
                  title: this.$t('staffIndex.staffFailureLog'),
                  sort: 1,
                  expand: true,
                  permission: '#staffFailureLog',
                },
                {
                  id:24,
                  parentId:22,
                  title: this.$t('staffIndex.stafferrorDel'),
                  sort: 2,
                  expand: true,
                  permission: '#stafferrorDel2',
                },
                {
                  id:25,
                  parentId:22,
                  title: this.$t('versionIndex.verListSettingsDel'),
                  sort: 3,
                  expand: true,
                  permission: '#verListSettingsDel1',
                },
              ],
            },
          ],
        },
        // {
        //   id:26,
        //   parentId:0,
        //   title: this.$t('navigationLeft.area'),
        //   sort: 3,
        //   expand: false,
        //   permission: '#area',
        //   children: [
        //     {
        //       id:27,
        //       parentId:26,
        //       title: this.$t('areaIndex.areaAddList'),
        //       sort: 1,
        //       expand: true,
        //       permission: '#areaAddList',
        //     },
        //     {
        //       id:28,
        //       parentId:26,
        //       title: this.$t('versionIndex.verListSettingsEdit'),
        //       sort: 2,
        //       expand: true,
        //       permission: '#verListSettingsEdit2',
        //     },
        //     {
        //       id:29,
        //       parentId:26,
        //       title: this.$t('versionIndex.verListSettingsDel'),
        //       sort: 3,
        //       expand: true,
        //       permission: '#verListSettingsDel2',
        //     },
            
        //   ],
        // },
        
        {
          id:30,
          parentId:0,
          title: this.$t('Attendance.AttendanceManage'),
          sort: 4,
          expand: false,
          permission: '#attendance',
          children: [
            {
              id:73,
              parentId:30,
              title: this.$t('Attendance.AttendanceAdd'),
              sort: 1,
              expand: true,
              permission: '#AttendanceAdd',
            },
            {
              id:74,
              parentId:30,
              title: this.$t('versionIndex.verListSettingsEdit'),
              sort: 2,
              expand: true,
              permission: '#verListSettingsEdit3',
            },
            {
              id:75,
              parentId:30,
              title: this.$t('versionIndex.verListSettingsDel'),
              sort: 3,
              expand: true,
              permission: '#verListSettingsDel3',
            },
            {
              id:76,
              parentId:30,
              title: this.$t('Attendance.AttendanceExport'),
              sort: 4,
              expand: true,
              permission: '#AttendanceExport',
            },
          ],
        },
        {
          id:31,
          parentId:0,
          title: this.$t('navigationLeft.visitors'),
          sort: 5,
          expand: false,
          permission: '#visitors',
          children: [
            {
              id:32,
              parentId:31,
              title: this.$t('visitorsIndex.visitorsAdd'),
              sort: 1,
              expand: true,
              permission: '#visitorsAdd',
            },
            {
              id:33,
              parentId:31,
              title: this.$t('staffIndex.stafferrorDel'),
              sort: 2,
              expand: true,
              permission: '#stafferrorDel3',
            },
            {
              id:34,
              parentId:31,
              title: this.$t('devicesListSettings.devListEdit'),
              sort: 3,
              expand: true,
              permission: '#devListEdit2',
            },
            {
              id:35,
              parentId:31,
              title: this.$t('devicesListSettings.devListDel'),
              sort: 4,
              expand: true,
              permission: '#devListDel1',
            }
          ],
        },
        {
          id:36,
          parentId:0,
          title: this.$t('navigationLeft.bodyTemperature'),
          sort: 6,
          expand: false,
          permission: '#temperature',
          children: [
            {
              id:37,
              parentId:36,
              title: this.$t('BodyTemperatureIndex.BodyTemAnimalRecord'),
              sort: 1,
              expand: true,
              permission: '#BodyTemAnimalRecord',
            },
          ],
        },
        {
          id:38,
          parentId:0,
          title: this.$t('navigationLeft.accessRecord'),
          sort: 7,
          expand: false,
          permission: '#accessRecord',
          children: [
            {
              id:85,
              parentId:38,
              title: this.$t('staffIndex.staffFailureLog'),
              sort: 1,
              expand: true,
              permission: '#staffFailureLog1',
            },
          ],
        },
        {
          id:39,
          parentId:0,
          title: this.$t('UlogI18n.UlogI18nTit'),
          sort: 8,
          expand: false,
          permission: '#uLog',
          children: [
            {
              id:40,
              parentId:39,
              title: this.$t('staffIndex.staffFailureLog'),
              sort: 1,
              expand: true,
              permission: '#staffFailureLog2',
            },
          ],
        },
        {
          id:41,
          parentId:0,
          title: this.$t('navigationLeft.permissions'),
          sort: 9,
          expand: false,
          permission: '#permissions',
          children: [
            {
              id:42,
              parentId:41,
              title: this.$t('permissionsIndex.UserTit'),
              sort: 1,
              expand: true,
              permission: '#UserTit',
              children: [
                {
                  id:43,
                  parentId:42,
                  title: this.$t('versionIndex.verAddBtn'),
                  sort: 1,
                  expand: true,
                  permission: '#verAddBtn',
                },
                {
                  id:44,
                  parentId:42,
                  title: this.$t('versionIndex.verListSettingsEdit'),
                  sort: 2,
                  expand: true,
                  permission: '#verListSettingsEdit4',
                },
                {
                  id:45,
                  parentId:42,
                  title: this.$t('versionIndex.verListSettingsDel'),
                  sort: 3,
                  expand: true,
                  permission: '#verListSettingsDel4',
                },
                {
                  id:46,
                  parentId:42,
                  title: this.$t('loginIndex.loginPhoneEdit'),
                  sort: 4,
                  expand: true,
                  permission: '#loginPhoneEdit',
                },
              ],
            },
            {
              id:47,
              parentId:41,
              title: this.$t('permissionsIndex.roleTitle'),
              sort: 2,
              expand: true,
              permission: '#roleTitle',
              children: [
                {
                  id:48,
                  parentId:47,
                  title: this.$t('permissionsIndex.roleAdd'),
                  sort: 1,
                  expand: true,
                  permission: '#roleAdd',
                },
                {
                  id:49,
                  parentId:47,
                  title: this.$t('versionIndex.verListSettingsEdit'),
                  sort: 2,
                  expand: true,
                  permission: '#verListSettingsEdit5',
                },
                {
                  id:50,
                  parentId:47,
                  title: this.$t('versionIndex.verListSettingsDel'),
                  sort: 3,
                  expand: true,
                  permission: '#verListSettingsDel5',
                },
              ],
            },
          ],
        },
        {
          id:51,
          parentId:0,
          title: this.$t('navigationLeft.TerminalVersion'),
          sort: 10,
          expand: false,
          permission: '#version',
          children: [
            {
              id:70,
              parentId:51,
              title: this.$t('versionIndex.verAddBtn'),
              sort: 1,
              expand: true,
              permission: '#verAddBtn1',
            },
            {
              id:71,
              parentId:51,
              title: this.$t('versionIndex.verListSettingsEdit'),
              sort: 2,
              expand: true,
              permission: '#verListSettingsEdit6',
            },
            {
              id:72,
              parentId:51,
              title: this.$t('versionIndex.verListSettingsDel'),
              sort: 3,
              expand: true,
              permission: '#verListSettingsDel6',
            },
          ],
        },
        {
          id:52,
          parentId:0,
          title: this.$t('navigationLeft.emailAdmin'),
          sort: 11,
          expand: false,
          permission: '#emailAdmin',
          children: [
            {
              id:53,
              parentId:52,
              title: this.$t('emailSetting.setting'),
              sort: 1,
              expand: true,
              permission: '#setting',
            },
          ],
        },
        // {
        //   id:54,
        //   parentId:0,
        //   title: this.$t('navigationLeft.companyManagement'),
        //   sort: 12,
        //   expand: false,
        //   permission: '#companyManagement',
        //   children: [],
        // },
        // {
        //   id:55,
        //   parentId:0,
        //   title: this.$t('navigationLeft.warningMessage'),
        //   sort: 13,
        //   expand: false,
        //   permission: '#warningMessage',
        //   children: [],
        // },
        {
          id:87,
          parentId:0,
          title: this.$t('navigationLeft.advertising'),
          sort: 13,
          expand: false,
          permission: '#advertising',
          children: [],
        },
      ],
      formRule: {
        RoleName: { required: true, validator: (rule, value, callback) => {
            this.formData.RoleName = this.formData.RoleName.replace(/\s*/g,"");
            if (this.formData.RoleName.length == 0) {
              return callback(new Error(this.$t('permissionsIndex.roleNameSky')))
            }else {
              return callback()
            }
          } , trigger: 'blur'
        },
        RoleMark: { required: true, validator: (rule, value, callback) => {
            this.formData.RoleMark = this.formData.RoleMark.replace(/\s*/g,"");
            if (this.formData.RoleMark.length == 0) {
              return callback(new Error(this.$t('permissionsIndex.roleRemarkSky')))
            }else {
              return callback()
            }
          } , trigger: 'blur'
        },
        dataPermiss: { required: true, validator: (rule, value, callback) => {
          if (this.dataPermissData.length == 0) {
            return callback(new Error(this.$t('permissionsIndex.roleSelper') ))
          } else {
            return callback()
          }

          } 
        },

      },
    }
  },
  computed: {
    ...mapState('depart', [
      'selectedDepart',
    ]),
  },

  mounted () {
    this.show = true
    if (this.editType === 'update') {

      console.log(this.editItem)
      this.formData.RoleName = this.editItem.roleName
      this.formData.RoleMark = this.editItem.remark
     
      if (this.editItem.roleMenu) {
        let roleMenuEach  = JSON.parse(this.editItem.roleMenu)
        // function chuli(arr,roleMenuEach){
            this.dataPermiss.forEach((item, index) => {
           roleMenuEach.forEach((item1, index1) => {
              // console.log('chunzai')
              // console.log(item.permission == item1.permission&&item1.checkedd)
              if (item.permission == item1.permission&&item1.checked){
                this.$set(item, 'checked', true)               
                // if(item1.checked==true){
                  // this.$set(item.children, 'checked', true)
                // }
              }
              else if(item.permission == item1.permission&&item1.checked==false){
                item.children= JSON.parse(JSON.stringify(item1.children))
              }
              // else if(item.permission == item1.permission&&item1.checked==false){
              //   if(item1.children){
              //     item1.children.forEach((v,i)=>{
              //       if(v.checked){

              //       }
              //     })
              //   }
              // }
            })
          })
        // }
        // if (item.permission == item1.permission){
        //         if(item1.checked==true){
        //           this.$set(item, 'checked', true)
        //         }else{
        //           if(item1.children){
        //             item.children.forEach(v=>{

        //             })
        //           }
        //         }
                
        //       }else{

        //       }
      
        // let data1 = this.$refs.tree.getCheckedAndIndeterminateNodes()

        this.dataPermissData =roleMenuEach
      }
    }  
  },

  methods: {
    getAllNode(arr, name, childName){
      let str = ''
      const arrs = []
      if (!arr) {
        return
      }
      if (!name) {
        name = 'id'
      }
      if (!childName) {
        childName = 'children'
      }
      const getChild = (arr) => {
        for (let i = 0; i < arr.length; i++) {
          arrs.push(arr[i][name])
          if (arr[i][childName] && arr[i][childName].length > 0) {
            getChild(arr[i].children)
          }
        }
        // str = arrs.join()
        // str = arrs.join()
        return arr
        // return str
      }
      return getChild(arr)
    },
    getPhoto,
    TreeChange () {
      let data1 = this.$refs.tree.getCheckedAndIndeterminateNodes()
      // let data2 = this.$refs.tree.getCheckedNodes()//获取被勾选的节点

      let list1=[]
      data1.forEach(v=>{
        if(v.parentId==0){
          list1.push(v)
        }
      })
      // let delList=[]
      // function dG(params) {
      //   params.forEach((v,i)=>{
      //     if(v.children&&v.children.length>0){
      //       dG(v.children)
      //     }else{
      //       if(!v.children&&v.checked==false){
      //         let a={
      //           id:v.id,
      //           parentId:v.parentId,
      //         }
      //         delList.push(a)
      //       // if(!v.children&&v.checked==false){
      //         // v.permission=''
      //         // v.parentId=''
      //         // v.id=''
      //         // v=null;
      //         // v={}
      //         // console.log('v',v)
      //         // v.splice(i,1)
      //       }
      //     }
      //   })
      // }
      // console.log('delList',delList)
      // dG(list1)
      this.dataPermissData = 
      list1
      
      // this.dataPermissData = data1
      console.log('this.dataPermissData')
      console.log(this.dataPermissData)
    },
    TreeChange1 () {
      //扩展remove方法
        Array.prototype.remove = function (val) {
          let index = this.indexOf(val);
          if (index > -1) {
            this.splice(index, 1);
          }
        };
        //======================================
        //获取整条数据链
        function getParent(array, childs, ids) {
          for (let i = 0; i < array.length; i++) {
            let item = array[i];
            if (Number(item.id) === Number(ids)) {
              childs.push(item);
              return childs;
            }
            if (item.children && item.children.length > 0) {
              childs.push(item);
              let rs = getParent(item.children, childs, ids);
              if (rs) {
                return rs;
              }
              else {
                childs.remove(item);
              }
            }
          }
          return false;
        }

        //获取所有选中节点
        let params = this.$refs.tree.getCheckedNodes();
        //所有数据
        let allData = ['所有数据'];
        //循环执行所有选中的节点链，放到arr1数组里
        let arr1 = [];
        for (let i = 0; i < params.length; i++) {
          //单条数据链
          var aData = getParent(allData, [], params[i].id);//方法入口在这里
          for (let y = 0; y < aData.length; y++) {
            //拆分成单个json数组放到arr1里
            arr1.push(aData[y]);
          }
        }
        console.log('aData',aData)
        //arr1去重 es6的set方法
        function dedupe(array) {
          return Array.from(new Set(array));
        }

        arr1 = dedupe(arr1);
        this.dataPermissData=arr1
        console.log('this.dataPermissData1')
        console.log(this.dataPermissData)
    },
    async handleSubmit () {
      
      const ifVlid = await this.$refs.form.validate()
      if (ifVlid && this.editType === 'create') {
        this.handleCreate()
      }
      if (ifVlid && this.editType === 'update') {
        this.handleUpdate()
      }
    },

    async handleCreate () {
      let formData = {
        roleName: this.formData.RoleName,
        remark: this.formData.RoleMark,
        roleMenu: JSON.stringify(this.dataPermissData),
      }
      console.log(formData)
      const { success,data } = await create(formData)
      if (success) {
        this.$Message.success(this.$t('devicesCreate.devCreateS'))
        this.ifUpdateUpper = true
        this.$_modal_close()
      } else {
        // this.$Message.error(this.$t('permissionsIndex.roleAddErr'))
        if(data=="权限组已存在"){
          this.$Message.error(this.$t('newDevices.createdAdminTip1'))
        }else if(data=="用户名已存在"){
          this.$Message.error(this.$t('newDevices.createdAdminTip2'))
        }else{
          this.$Message.error(data)
        }
      }
    },
    async handleUpdate () {
      let formData = {
        roleName: this.formData.RoleName,
        remark: this.formData.RoleMark,
        roleMenu: JSON.stringify(this.dataPermissData),
        id: this.editItem.id,
      }
      // console.log('handleUpdate.dataPermissData')
      // console.log(this.dataPermissData)
      const { success,data} = await update(formData)
      if (success) {
        this.$Message.success(this.$t('devicesCreate.devCreateUpdateS'))
        this.ifUpdateUpper = true
        this.$_modal_close()
      } else {
        if(data=="权限组已存在"){
          this.$Message.error(this.$t('newDevices.createdAdminTip1'))
        }else if(data=="用户名已存在"){
          this.$Message.error(this.$t('newDevices.createdAdminTip2'))
        }else{
          this.$Message.error(data)
        }
      }
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ivu-date-picker-cells-header span{
  white-space: nowrap;
  font-size: 13px;
}
.alertTit{
  width: 90%;
  z-index: 10000;
  margin: 4px auto 20px;
  line-height: 14px;
}
</style>
